const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    RegionCustomItem:"全部",   //自定义数据项
    Type: ["全部", "临街门面", "市场门面", "停车场"],    //类型数据
    TypeVal:"", //类型值
    Rent: ["出租","求租"],   //租值
    RentStatus: 0, //租值状态 0出租 1求租
    // showInitLoading:false, //初始加载动画
    loadingStatus:0,   //上拉加载状态 0停止 1开启 2没有更多数据
    loadingVal: [0,"加载更多中...","没有更多啦~"],
    imgSrc: SERVICE.imgUrl,   //图片路径
    // rentList: [],
    showNoData: false
  },
  onLoad(){
    // this.InitData();
    // this.InitRentList();
  },
  
  onShow(){
    this.InitData();
    this.InitRentList();
  },

  /*初始数据*/
  InitData(){
    this.setData({
      page: 1,   //分页数据
      genre: "",   //类型
      rentList: [],
      region:["","",""],   //位置数据
    });
  },

  /*加载初始数据*/ 
  InitRentList(){
    let _this = this, td = this.data;
    this.setData({
      showInitLoading: false
    });
    SERVICE.POST(td.RentStatus == 0 ? SERVICE.URL.rentList : SERVICE.URL.Magdebrug,{
      page:td.page,
      genre: td.TypeVal,
      province: td.region[0],
      city: td.region[1],
      county: td.region[2],
    },(status,res)=>{
      console.log(res);
      if (res.data.status === "nothing"){
        _this.setData({
          showInitLoading: true,          
          rentList:null,
          showNoData: true
        });
      }else{
        _this.setData({
          showInitLoading: true,
          showNoData: false,
          rentList: res.data
         
        });
      }
    });
  },

  /*分享*/ 
  onShareAppMessage(){

  },
  
  /*下拉刷新*/
  onPullDownRefresh(e){
    let _this = this, td = this.data;
    td.loadingStatus = 0;
    SERVICE.POST(td.RentStatus == 0 ? SERVICE.URL.rentList : SERVICE.URL.Magdebrug, {
      page: td.page = 1,
      genre: td.TypeVal,
      province: td.region[0],
      city: td.region[1],
      county: td.region[2]
    }, (status, res) => {
      wx.stopPullDownRefresh();    //停止下拉        
      if (res.data.rentalList == "nothing") {
        _this.setData({
          Loadingtatus: "暂无数据哦~",
          rentList: null,
          showNoData: true
        });
      } else {
        _this.setData({
          showNoData: false,
          rentList: res.data
        });
      }
    });
  }, 

  /*上拉刷新*/
  onReachBottom(e){
    let _this = this, td = this.data; 
    // console.log(td.page, td.rentList.all_page);  
    if (td.page < td.rentList.all_page && td.loadingStatus == 0){
      _this.setData({
        loadingStatus: 1
      });
      SERVICE.POST(td.RentStatus == 0 ? SERVICE.URL.rentList : SERVICE.URL.Magdebrug, {
        page: ++ td.page,
        genre: td.TypeVal,
        province: td.region[0],
        city: td.region[1],
        county: td.region[2],
      }, (status, res) => {
          if(res.data.status === "success"){
            if (td.RentStatus == 0) {
              td.rentList.rentalList = td.rentList.rentalList.concat(res.data.rentalList);
              _this.setData({
                loadingStatus: 0,
                rentList: td.rentList,
              });
            } else if (td.RentStatus == 1) {
              td.rentList.demandList = td.rentList.demandList.concat(res.data.demandList);
              _this.setData({
                loadingStatus: 0,
                rentList: td.rentList
              });
            }
          }
      });
     
    } else if (td.page == td.rentList.all_page){
      _this.setData({
        loadingStatus: 2
      });
    }
  
  },
   
  /*区域选择*/ 
  RegionChange(e){
    let region = e.detail.value;
    for (let i = 0; i < region.length; i++){
      if (region[i] == "全部"){
        region[i] = "";
      }
    }
    this.setData({
      region,
      loadingStatus: 0,
      page:1
    });
    this.InitRentList();
  },
  
  /*类型选择*/
  PickerChange(e){
    let td = this.data;
    this.setData({
      TypeVal: e.detail.value[0] == 0 ? '' : td.Type[e.detail.value[0]],
      loadingStatus: 0,
      page: 1
    });
    this.InitRentList();    
  },

  /*租值类型选择*/
  RentChange(e){
    let td = this.data, RentStatus = e.detail.value[0];
    this.setData({
      RentStatus,
      loadingStatus: 0,
      page: 1
    });
    if (RentStatus == 0) {
      wx.setNavigationBarTitle({
        title: "车位出租",
      });
    } else if (RentStatus == 1) {
      wx.setNavigationBarTitle({
        title: "车位求租",
      });
    }
    this.InitData();
    this.InitRentList();
  } 
});